import { ScanPage } from "./scan/scan";
import { Component, Injector, AfterViewInit } from "@angular/core";
import { IonicPage, App } from "ionic-angular";
import { BasePage } from "../basePage";
import { UserService } from "./../../providers/UserService";
import { StorageService } from "./../../providers/StorageService";
import { PIC_URL } from "./../../providers/Config";
import { TabsPage } from "../tabs/tabs";
 import { GalleryModal } from 'ionic-gallery-modal';

// import { BackButtonService } from '../../providers/backButton';
// import { Platform } from 'ionic-angular';
@IonicPage({ name: "personal" })
@Component({
  selector: "page-personal",
  templateUrl: "personal.html"
})
export class PersonalPage extends BasePage {
  showScan: boolean;
  number = 0;
  isLogin: boolean;
  showShop: number;
  nail=0;
  nothing: number;
  nailLevel: number;
  noneLevel: number;
  PageTabs;
  picture = "";
  loadPic = "";
  nickname = "";

  level = null;
  shopLevel = null;
  /**获取到用户的个人信息 */
  userinfo: any;
  /**发送到后台的数据 */
  user = {};
  param2 = {
    user_id: 0
  };
  reading = [];
  orderlistCount=0; //消费者订单相关
  orderlistCount2=0; //消费者订单相关
  Scan2;
  /*保存当前店主是否缴纳保证金*/
  hasPayCashDeposit: boolean;
  private photos: any[] = [];

  shopLife="";  //美甲店的审核状态显示的字
  isNailPassing:boolean=false;   //美甲师审核中的遮罩层
  isShopPassing:boolean=false;    //美甲店审核中的遮罩层
  userData:any  //用户信息，在这个页面登录后的信息，实时
  myBalance="";   //如果是消费者就不是我的余额而是我的账单

  constructor(
    injector: Injector,
    public userService: UserService,
    public stc: StorageService,
    public app: App
  ) {
    super(injector);
    // this.isLogin=false;
    this.userinfo = this.stc.read("userinfo");

    // platform.ready().then(() => {
    //   this.backButtonService.registerBackButtonAction(null);
    // });
  }
  ngAfterViewInit() {
    this.Scan2=document.getElementsByTagName("ng-component")[0] as HTMLDivElement;
    this.Scan2.style.display='block';
  }
 

  // 这个时候页面刚刚开始切换。你可以在这时对页面的数据进行预处理，这个钩子是每次都会调用的。
  ionViewDidLoad() {
    this.loadPic = PIC_URL;
    //如果没有登录就跳转到登录页面
    if (this.userinfo == null) {
      //this.navCtrl.push("LoginPage");
      this.isLogin = true;
      // this.navCtrl.popToRoot();
      
      let modal = this.modalCtrl.create("LoginPage");
      modal.onDidDismiss(data => {
        // this.navCtrl.setRoot(TabsPage);
        // this.viewCtrl.dismiss();
      });
      modal.present();
      return;
    } else {
      this.isLogin = false;
      this.myinfo();
      this.doLogin();
    }
    
  }

  ionViewWillEnter(){
    this.nail=0;
    if(this.userinfo != null){
      this.doLogin();
      this.myinfo();
    }
  }
  ionViewDidEnter() {
    if (this.userinfo != null) {
      this.getMyOrderListAPI();
      this.getMyOrderListAPI2();
    }
  }
  ionViewDidLeave() {}

  /**个人资料 */
  personalCLI() {
    console.log(this.picture);
    let modal = this.modalCtrl.create("PersonalinfoPage", {
      user_avator: this.picture
    });
    modal.onDidDismiss(data => {
      this.myinfo();
    });
    modal.present();
  }

  /**跳转登录页面 */
  toLogin() {
    let modal = this.modalCtrl.create("LoginPage");
    modal.onDidDismiss(data => {});
    modal.present();
  }
  /**跳转到扫码页面 */
  // gotoScan() {
  //    this.navCtrl.setRoot('ScanPage')
  // }
  gotoScan() {
    this.navCtrl.push('ScanPage',null,{'animate':false})
    // this.navCtrl.setRoot('ScanPage') ;
    // this.navCtrl.popAll();
    // let modal = this.modalCtrl.create("ScanPage");
    // modal.present()
    //
    // this.app.getRootNav().setRoot("ScanPage");
   
  
 
  }
  ionViewWillUnload() {
    console.log("页面销毁了");
    // this.PageTabs.style.display='none'
  }
  /**申请加入店铺 */
  gojoin() {
    let modal = this.modalCtrl.create("ApplyJoinShopPage");
    modal.onDidDismiss(data => {
    });
    modal.present();
  }
  /** 跳转到消费者订单 通过传入不同的值跳转到不同的子页面 */
  consumerorderCLI(stu) {
    console.log("当前的状态码为" + stu);
    /* 
      0 表示 所有的订单  1表示待收货订单  2表示待评价订单
    */
   //this.navCtrl.push("ConsumerorderPage", { stu: stu })
    let modal = this.modalCtrl.create("ConsumerorderPage", { stu: stu });
    modal.onDidDismiss(data => {
      this.getMyOrderListAPI();
      this.getMyOrderListAPI2();
    });
    modal.present();
  }
  /**个人资料  */
  getmyInfo() {
    let modal = this.modalCtrl.create("PersonalinfoPage");
    modal.onDidDismiss(data => {});
    modal.present();
  }

  /**美甲店订单 */
  shoporderCLI(stu) {
    console.log(stu);
    let modal = this.modalCtrl.create("ShopPage", { stu: stu });
    modal.onDidDismiss(data => {});
    modal.present();
  }

  /**我的美图美款 */
  goNailPicture() {
    let modal = this.modalCtrl.create("MypicturePage");

    modal.onDidDismiss(data => {});
    modal.present();
  }
  /**余额 */
  gotoBalance() {
    console.log(this.userData);
    if(this.userData['user_type']=="1"){
      let identity="消费者";
      let modal = this.modalCtrl.create("BillPage",{identity:identity});
      modal.onDidDismiss(data => {});
      modal.present();
    }else{
      let modal = this.modalCtrl.create("BalancePage");
      modal.onDidDismiss(data => {});
      modal.present();
    } 
  }

  /**消费者 美甲师 美甲店 切换 */
  /**消费者 */
  toConsumer() {
    this.nothing = 1;
    this.nail = 0;
    this.showShop = 0;
  }
  /**美甲师 */
  toNail() {
    this.nothing = 0;
    this.nail = 2;
    this.showShop = 0;
  }
  /**美甲店 */
  toShop() {
    this.nothing = 0;
    this.nail = 0;
    this.showShop = 3;
  }

  /**我的关注*/
  followCLI() {
    let modal = this.modalCtrl.create("FollowPage");
    modal.onDidDismiss(data => {
      this.myinfo();
    });
    modal.present();
  }

  /**我的收藏 */
  collectionCLI() {
    let modal = this.modalCtrl.create("CollectionPage");
    modal.onDidDismiss(data => {
      this.myinfo();
    });
    modal.present();
  }

  /**我的粉丝 */
  myzanCLI() {
    let modal = this.modalCtrl.create("CuszansPage");
    modal.onDidDismiss(data => {
      this.myinfo();
    });
    modal.present();
  }

  /**美甲师认证 */
  nailauthCLI() {
    let modal = this.modalCtrl.create("TeacherNailPage");
    modal.onDidDismiss(data => {
      this.doLogin();
    });
    modal.present();
  }
  alertTip(){
    this.showAlert("您已提交申请，请待认证通过!")
  }
  /**设置 */
  settingCLI() {
    let modal = this.modalCtrl.create("SettingPage");
    modal.onDidDismiss(data => {});
    modal.present();
  }
  // /*待评价 */

  /** 自动登录 */
  async doLogin() {
    this.userinfo["user_token"] = this.userinfo.user_id;
    //请求后台登陆
    let result = await this.userService.doLoginAPI(this.userinfo);
    if(result.code == 1){
      this.userData = result.data;
      console.log(this.userData)
      let userType = this.userData.user_type.split(",");
      console.log(userType.toString());
      //判断身份
      if (
        userType.toString().indexOf("1") != -1 &&
        userType.toString().indexOf("2") == -1 &&
        userType.toString().indexOf("3") == -1
      ) {
        this.noneLevel = 1;
        this.nailLevel = 1;
        this.nothing = 1;
        this.shopLevel = 3;
        console.log("这是消费者");
        this.myBalance="我的账单"
      } else if (
        userType.toString().indexOf("1") != -1 &&
        userType.toString().indexOf("2") != -1 &&
        userType.toString().indexOf("3") == -1
      ) {
        this.nailLevel = 2;
        this.shopLevel = 3;
        this.nothing = 1;
        this.noneLevel=2;
        console.log("这是美甲师");
        this.myBalance="我的余额"
      } else if (
        userType.toString().indexOf("1") != -1 &&
        userType.toString().indexOf("2") == -1 &&
        userType.toString().indexOf("3") != -1
      ) {
        this.shopLevel = 4;
        this.noneLevel = 1;
        this.nothing = 1;
        this.nailLevel = 1;
        console.log("这是美甲店");
        this.myBalance="我的余额"
      } else if (
        userType.toString().indexOf("1") != -1 &&
        userType.toString().indexOf("2") != -1 &&
        userType.toString().indexOf("3") != -1
      ) {
        this.nailLevel = 2;
        this.shopLevel = 4;
        this.nothing = 1;
        this.noneLevel=2;
        console.log("这是三个身份都有");
        this.myBalance="我的余额"
      }
      //审核状态
      if(this.userData.artist_status && this.userData.artist_status == 0){
        this.isNailPassing = true;
      }else{
        this.isNailPassing = false;
      }
      if(this.userData.shop_status && this.userData.shop_status ==0){
        this.isShopPassing=true;
      }else{
        this.isShopPassing=false;
      }
    }
    this.stc.write("userVo", result.data);
  }

  /**获取后台数据 */
  async myinfo() {
    let param = { user_id: this.userinfo["user_id"] };
    let loader = this.loadingCtrl.create({
      content: ""
    });
    loader.present();
    let result = await this.userService.doMyInfoAPI(param);
    console.log(result)
    loader.dismiss();
    if (result.code != 1) {
      this.showAlert(result.msg);
    } else {
      this.user = result.data;
      this.picture = result.data.user_info.user_avator;
      this.nickname = result.data.user_info.user_nickname;
      this.hasPayCashDeposit =
        result.data.user_info.is_bail == 1 ? true : false;
      console.log(this.hasPayCashDeposit);
      
    }
  }

  /**获取后台相应数据 */
  async getMyfan() {
    let fanslist = [];
    let param = {
      user_id: this.userinfo["user_id"]
    };
    let result = await this.userService.getFans(param);
    console.log(result);

    if (result.code == 1001) {
      this.number = 0;
    } else {
      if (this.userinfo["user_type"] == 1) {
        this.number = result.data.artist_fans;
      } else if (this.userinfo["user_type"] == 2) {
        this.number = result.data.shop_fans;
      }
    }
  }

  /*获取消息数量*/
  // async getuserMsg() {
  //   this.param2.user_id = this.userinfo['user_id'];
  //   console.log(this.param2.user_id)
  //   let result = await this.userService.getMsg(this.param2);
  //    console.log(result);
  //   if (!result) {
  //     this.showAlert(result.msg);
  //   } else {
  //     console.log(result);
  //     for (let i = 0; i < result.messages.length; i++) {
  //       if (result.messages[i].is_read == 0) {
  //         this.reading.push(result.messages[i]);
  //         console.log(this.reading.length);
  //       }
  //     }
  //   }
  // }

  /**店铺管理 */
  mangershop() {
    //this.navCtrl.push("MyShopPage");
    let modal = this.modalCtrl.create("MyShopPage");
    modal.onDidDismiss(data => {

    });
    modal.present();
  }
  /**我的优惠券 */
  toDiscountCoupon() {
    //this.navCtrl.push("DiscountPage");
    let modal = this.modalCtrl.create("DiscountPage");
    modal.onDidDismiss(data => {
    });
    modal.present();
  }

  /**跳转美甲店认证 */
  shop() {
    let modal = this.modalCtrl.create("ShopApprovePage");
    modal.onDidDismiss(data => {

    });
    modal.present();
    //this.navCtrl.push("ShopApprovePage");
  }
  /*购物车*/
  gotoBuycar() {
    this.navCtrl.push("BuycarPage");
  }
  /*消息中心*/
  gotomsg() {
    this.navCtrl.push("MsgcenterPage");
  }
  /**跳转美甲师订单 */
  nailerOrderCli() {
    let modal = this.modalCtrl.create("NailerOrderPage");
    modal.onDidDismiss(data => {
    });
    modal.present();
    //this.navCtrl.push("NailerOrderPage");
  }

  /*缴纳保证金的函数*/
  toPayCashDeposit(stu) {
    let shop_id = this.userinfo.shop_id;

    if (stu == 0) this.navCtrl.push("PayCashDepositPage", { shop_id });
    else this.navCtrl.push("PayCashDepositByMoneyPage", { shop_id });
  }

  //获取待服务数量
  async getMyOrderListAPI() {
    let param = { user_id: this.userinfo["user_id"], order_type: 1 };
    // let loader = this.loadingCtrl.create({
    //   content: ""
    // });
    let result = await this.userService.getMyOrderListAPI(param);
    //loader.dismiss();
    console.log(result);
    //成功
    if (result.code == 1) {
      this.orderlistCount = result.count;
    }
  }

  /**获取待评价数量 */
  async getMyOrderListAPI2() {
    let param = { user_id: this.userinfo["user_id"], order_type: 2 };
    let result = await this.userService.getMyOrderListAPI(param);
    if (result.code == 1) {
      this.orderlistCount2 = result.count
    }
  }

  openModal(event, showImage) {
    console.log(showImage)
    this.photos=[];
    this.photos.push({
      url: this.loadPic + showImage,
    });
    console.log(this.photos);
    let modal = this.modalCtrl.create(GalleryModal, {
      photos: this.photos,
      initialSlide: 2, // The second image
    },{
      enterAnimation: 'modal-scale-enter',
      leaveAnimation: 'modal-scale-leave'
    });
    modal.onDidDismiss(data => {
      
    });
    modal.present();
  }
}
